package org.ruwang.springai_hello.controller;

import lombok.RequiredArgsConstructor;
import org.ruwang.springai_hello.repository.ChatHistoryRepository;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
public class CustomerServiceController {

    private final ChatClient serviceClient;

    private final ChatHistoryRepository chatHistoryRepository;

    //目前SpringAI的OpenAI客户端与阿里云百炼存在兼容性问题，
    // 所以FunctionCalling功能无法使用stream模式，这里使用call来调用。
    @RequestMapping(value = "/service",produces = "text/html;charset=utf-8")
    public String service(String prompt,String chatId){
        chatHistoryRepository.save("service",chatId);

        return serviceClient.prompt()
                .user(prompt)
                .advisors(a->a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,chatId))
                .call()
                .content();
    }
}
